package com.ypy.context.example;

import com.ypy.context.beans.DefaultListableApplicationContext;
import com.ypy.context.dao.UserDao;
import com.ypy.context.entity.User;
import com.ypy.context.service.UserService;


import java.net.URLDecoder;

/**
 * @author: yeskip
 * @description: Test entry point
 */
public class ExampleTest {
	public static void main(String[] args) {
		DefaultListableApplicationContext context = new DefaultListableApplicationContext("com.ypy.context");
		UserService userService = (UserService) context.getBean("userService");
		UserDao userDao = context.getBean("userDaoImpl", UserDao.class);
		User user = context.getBean("user", User.class);
		boolean existFlag = context.exists("aaa", UserService.class);
		System.out.println("从Bean容器中获取user: " + user);
		System.out.println("UserService exists ? " + existFlag);
		System.out.println("从Bean容器中获取aaa对象地址: " + userService);
		System.out.println("从Bean容器中获取userDao对象地址: " + userDao);
		userService.add();
	}

	public static void fetchUrl() {
		String basePackage = "com.ypy.context";
		String decodeUrlFullPath = URLDecoder.decode(
				Thread.currentThread()
						.getContextClassLoader()
						.getResource(basePackage.replaceAll("\\.", "/"))
						.getPath()
		);
		System.out.println(decodeUrlFullPath);
		String baseUrl = decodeUrlFullPath.substring(0, decodeUrlFullPath.length() - basePackage.length());
		System.out.println(baseUrl);
	}
}
